﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class admin_modi : System.Web.UI.Page
{
    DB DBAdmin = new DB();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"].ToString());
        if (!IsPostBack)
        {
            TextBox3.Text = "";
            Label1.Text = "";
            string strtitle = "select voteTitle from voteMaster where id='" + id + "'";
            string vottit = DBAdmin.executeGetReturn(strtitle);
            TextBox1.Text = vottit;
            string strcontent = "select votecontent from voteMaster where id='" + id + "'";
            string votcont = DBAdmin.executeGetReturn(strcontent);
            if (votcont != null)
            {
                TextBox2.Text = votcont;
            }
            string strcount = "select count(voteDetailsID) from voteDetails where id='" + id + "'";
            int count = Convert.ToInt32(DBAdmin.executeGetReturn(strcount));
            string strdet = "select * from voteDetails where id='" + id + "'";
            SqlDataReader sdr = DBAdmin.getSdr(strdet);     
            int i = 0;
            while (sdr.Read())
             {
               if (i == count - 1)
               {
                   TextBox3.Text += sdr.GetString(2).Replace("\r", "");
                   Label1.Text += Convert.ToString(sdr.GetInt32(1));
                }
               else
                {
                   TextBox3.Text += sdr.GetString(2) + "\n";
                   Label1.Text += Convert.ToString(sdr.GetInt32(1)) + "|";
                }
                    i = i + 1;
              }
            sdr.Close();
            string strconfig = "select voteMultiple from voteMaster where id='" + id + "'";
            int IsMultiple = Convert.ToInt32(DBAdmin.executeGetReturn(strconfig));
            if (IsMultiple > 0)
            {
                Checkbox1.Checked = true;
            }
            }  
        }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"].ToString());
        string trim = TextBox3.Text.Replace("\n", "|");
        string[] count = trim.Split('|');
        string[] count2 = Label1.Text.Split('|');
        int IsMuptiple = 0;
        if (Checkbox1.Checked)
        {
            IsMuptiple = 1;
        }

        if (count.Length != count2.Length)
        {
            Label2.Text = "修改不能修改投票项数,其中选项个数为" + count.Length.ToString() + ",原选项个数为" + count2.Length.ToString() + "";
        }
        else
        {
            string sqlstr = "update voteMaster set voteTitle='" + TextBox1.Text + "',votecontent='" + TextBox2.Text + "',voteMultiple='"+IsMuptiple +"' where id='" + id + "'";
            DBAdmin.executeNonQuery(sqlstr);

            for (int i = 0; i < count.Length; i++)
            {
                sqlstr = "update voteDetails set voteItem='" + count[i].ToString() + "' where voteDetailsID='" + count2[i].ToString() + "'";
                DBAdmin.executeNonQuery(sqlstr);
            }
            Response.Redirect("mana.aspx" );
        }
    }
}
